<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$merchid=intval($_RQ['merchid']);
	if($merchid || !$_RQ['ismerch']){
		$this->session('merchid',$merchid);
	}
	if($_RQ['ismerch']){
		$merchid=$_SESSION['merchid'];
		$this->session('merchid',$merchid);
	}
	if($this->session('merchid')){
		$merch=pdo_get('jy_merch_user',array('id'=>$this->session('merchid'),'uniacid'=>$_SESSION['uniacid']));
		$return['plugin_icon']=$merch['logo'];
	}
	$return['item']=array('from'=>'taobao','merchid'=>$merchid);
	$return['htmls']=array (
		  'type' => 'edit',
		  'post' => 'post',
		  'edit_tab' => '采集商品',
		  'edits' => array (
		    array (
		      'title' => '采集商品',
		      'list' => array (
				array ('type' => 'tip','title' => '<red>[温馨提示]：助手采集是非api接口采集，存在很多不确定性，采集过程可能不稳定！</red></br>由于淘宝防抓取措施，淘宝助手获取不到商品淘宝价。所以，统一用商品标价填充，如果抓取商品无标价，则统一填充0'),
				array ('name' => 'from','type' => 'radio','title' => '选择类型','options'=>'from','help'=>''),
				array ('name' => 'url','type' => 'string','title' => '链接 或 itemId','help'=>'例如商品链接为: https://item.taobao.com/item.htm?id=586126013807或:http://detail.tmall.com/item.htm?id=586126013807直接输入商品链接或输入商品ID:586126013807'),
				array ('name'=>'cate','title'=>'分类','type'=>'goodgroup','options'=>'cid'),
			 ),
		    ),
		  ),
	);
	//获取分类
	$params=array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0);
	$return['pcate']=pdo_getall('jy_weishop_goods_group',$params);
	$c=array();
	foreach($return['pcate'] as $v){
		$n=$v['title'];
		$son=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$v['id']));
		if($son){
			foreach($son as $s){
				$d=$s['id'];
				$c[$n][$d]=$s['title'];
			}
		}else{
			$c[$n][0]='';
		}
	}
	$return['cid']=$c;
	$return['from']=array('taobao'=>'淘宝','tmall'=>'天猫');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$merchid=$postdata['merchid'];	
	set_time_limit(0);
	$ret = array();
//	$_RQ['url']='http://item.taobao.com/item.htm?id=557143503333';
	$url = $postdata['url'];
	$cates = $postdata['cate'];
	if(!$cates){
		exi('请选择分类！'.$postdata['url'],'error');
	}
	$from =  $postdata['from']?:'taobao';
	if (is_numeric($url)) {
			$itemid = $url;
	}else{
		preg_match('/id\\=(\\d+)/i', $url, $matches);
			if(isset($matches[1])) {
				$itemid = $matches[1];
			}
	}	
	if (empty($itemid)) {
			exi('未获取到 itemid!','error');
	}
	if ($from == 'all') {
			$ret = $this->model('model')-get_item_taobao($itemid, $url, $cates,$merchid);
	}else if ($from == 'tmall') {
			$ret =$this->model('model')->get_item_tmall_bypage($itemid, $url, $cates,$merchid);
	}else if($from == 'taobao'){
			$ret =$this->model('model')->get_item_taobao($itemid, $url, $cates,$merchid);
	}
	//plog('taobao.main', '淘宝抓取宝贝 淘宝id:' . $itemid);		
	if($ret['result']==1){
			exi('采集商品成功');
	}else{
			exi('采集失败：'.$ret['error'],'error');
	}
		
}
?>